package com.jn.erp.web.api.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.web.api.student.entity.StudentUser;
import com.jn.erp.web.api.student.mapper.StudentUserMapper;
import com.jn.erp.web.api.student.service.StudentUserService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

import static com.jn.erp.security.utils.SecurityUtils.getUserId;

@Service
public class StudentUserServiceImpl extends ServiceImpl<StudentUserMapper, StudentUser> implements StudentUserService {



    @Override
    public StudentUser existOrCreate(String mobile,Long schoolId) {
        StudentUser user = this.getByMobile(mobile);
        if (user == null) {
            user = new StudentUser();
            user.setMobile(mobile);
            user.setAddTime(LocalDate.now());
            user.setSchoolId(0L);
            this.save(user);
        }
        return user;
    }
    @Override
    public StudentUser getByMobile(String mobile) {
        QueryWrapper<StudentUser> qw = new QueryWrapper();
        qw.eq("mobile", mobile);
        return (StudentUser)this.getOne(qw);
    }

    @Override
    public StudentUser existOrCreate(StudentUser studentUser) {
        String mobile = studentUser.getMobile();
        StudentUser user = getByMobile(mobile);
        if (user == null) {
            studentUser.setMobile(mobile);
            studentUser.setAddTime(LocalDate.now());
            studentUser.setCreator(getUserId());
            this.save(studentUser);
        } else {
            studentUser.setId(user.getId());
        }
        return studentUser;
    }


}
